package com.swedne.test.voice;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;

import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;

import com.swedne.test.R;

import java.io.IOException;

public class VoiceControl extends Activity {

    private View btnStart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_voice);
        AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        int max = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
        int current = mAudioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL);

        SeekBar bar_volume = (SeekBar) this.findViewById(R.id.setup_volume_seek);
        bar_volume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar arg0, int progrees, boolean fromUser) {
                if (fromUser) {
                    AudioManager m = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                    m.setStreamVolume(AudioManager.STREAM_VOICE_CALL, progrees, 0);
//                    m.setStreamVolume(AudioManager.STREAM_RING, progrees, 0);
//                    m.setStreamVolume(AudioManager.STREAM_ALARM, progrees, 0);
//                    m.setStreamVolume(AudioManager.STREAM_MUSIC, progrees, AudioManager.FLAG_PLAY_SOUND);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar arg0) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar arg0) {
            }
        });
        bar_volume.setMax(max);
        bar_volume.setProgress(current);


        btnStart = findViewById(R.id.btn_start);
        btnStart.setOnClickListener(v -> {
            play("/dnake/bin/prompt/alarm.wav", false, null, true);
        });
        findViewById(R.id.btn_start_media).setOnClickListener(v -> {
            play("/dnake/bin/prompt/modify_success.wav", false, null, false);
        });

    }

    public static MediaPlayer play(String url, Boolean looping, MediaPlayer.OnCompletionListener listener, boolean isVoice) {
        MediaPlayer mp = new MediaPlayer();
        try {
            mp.setDataSource(url);
            if (isVoice) {
                mp.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
            }
            mp.prepare();
            mp.setLooping(looping);
            mp.start();
            if (listener != null) {
                mp.setOnCompletionListener(listener);
            } else {
                mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer p) {
                        p.reset();
                        p.release();
                    }
                });
            }
            return mp;
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (mp != null) {
            mp.reset();
            mp.release();
        }
        return null;
    }


}
